package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.GameScene;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.utility.AircraftUtility;
import java.io.IOException;

import com.sun.speech.freetts.Voice; 
import com.sun.speech.freetts.VoiceManager;

/**
 *
 * @author deb kanti
 */
public class CommandAircraft extends javax.swing.JFrame 
{
    private GameScene gameScene;
    
    public CommandAircraft(GameScene gameScene) 
    {
        initComponents();
        jTextFieldCommand.addKeyListener(new CommandKeyListener());
        this.gameScene = gameScene;
        jTextFieldCommand.requestFocusInWindow();	
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaCommandLog = new javax.swing.JTextArea();
        jTextFieldCommand = new javax.swing.JTextField();
        jButtonClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jToggleButton1.setText("Voice");

        jTextAreaCommandLog.setEditable(false);
        jTextAreaCommandLog.setColumns(20);
        jTextAreaCommandLog.setFont(new java.awt.Font("Monospaced", 1, 13)); // NOI18N
        jTextAreaCommandLog.setRows(5);
        jScrollPane1.setViewportView(jTextAreaCommandLog);

        jTextFieldCommand.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jToggleButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jTextFieldCommand)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonClose)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jToggleButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jTextFieldCommand, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jButtonClose)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        //this.setVisible(false);
        //dispose();        
    }//GEN-LAST:event_jButtonCloseActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CommandAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CommandAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CommandAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CommandAircraft.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new CommandAircraft(null).setVisible(true);
            }
        });
    }
    
    class CommandKeyListener implements  KeyListener
    {	
	private String command;	
        
        public CommandKeyListener()
        {
            command = "";
        }

        @Override
        public void keyTyped(KeyEvent e) 
        {
            
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            
        }

        @Override
        public void keyReleased(KeyEvent e) 
        {	    
            String keyPressed = String.valueOf(e.getKeyChar());           
            
            if(keyPressed.equals("\n"))
            {   
                if(AircraftUtility.compareWithCommandRegEx(command))
                {
                    String callSignCommand = command.substring(0, 3); 
//		    jTextAreaCommandLog.setText(callSignCommand +"\n");
//		    //System.out.println(callSignCommand);
                    boolean flagAircraftFound=false;
                    for(Aircraft aircraft: gameScene.airCraftList)
                    {
                        if(aircraft.getAircraftState().getCallSign().equals(callSignCommand))
                        {
                            jTextAreaCommandLog.setText(jTextAreaCommandLog.getText()+command+"\n");                            
                            aircraft.executeCommand(command);    
                            flagAircraftFound = true;
                        }
                    }
                    
                    if(flagAircraftFound==false)
                    {
                        jTextAreaCommandLog.setText(
				jTextAreaCommandLog.getText()+"AIRCRAFT NOT FOUND!"+"\n");
                    }
                }
                else
                {
                    jTextAreaCommandLog.setText(
			    jTextAreaCommandLog.getText()+"WRONG COMMAND!"+"\n");                   
                }
                
                command="";
                jTextFieldCommand.setText("");
                
            }
            else
            {
                command += keyPressed;
            }
            
            ////System.out.println("Current command: " + command);
        } // end of keyPressed 
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaCommandLog;
    private javax.swing.JTextField jTextFieldCommand;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables
}
